﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using ViewModels;

namespace LibraryMIS_SE01.UC
{
    public partial class BorrowBook : UserControl
    {
        BorrowBLL bll;//定义Borrow的业务逻辑类
        DataSet ds;//定义一个数据集
        UserVM vm;//定义用户视图模型类

        public BorrowBook()
        {
            InitializeComponent();
            bll = new BorrowBLL();
            ds = new DataSet();
        }
        /// <summary>
        /// 获取学生相片
        /// </summary>
        /// <param name="stuID">学生的ID</param>
        private void GetStuPic(string stuID)
        {
            // 如果用户相片组件有图片，进行组件图片清空处理
            if (this.pictureBoxUserPic != null)
                this.pictureBoxUserPic.Image = null;

            var obj = bll.GetStuPic(stuID);//根据用户ID进行Photo的查询

            if (obj != null)
                this.pictureBoxUserPic.Image = Image.FromStream(obj);
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            string stuno = this.textBoxStuNo.Text.Trim();//获取用户输入的学号
                        
            ds = bll.GetUserInfoByStuNo(stuno);//根据学号进行用户信息查询

            if (ds.Tables[0].Rows.Count > 0)
            {
                vm = new UserVM()
                {
                    UserID = Guid.Parse(ds.Tables[0].Rows[0][0].ToString()),
                    StuNo = ds.Tables[0].Rows[0][1].ToString(),
                    Name = ds.Tables[0].Rows[0][2].ToString(),
                    Gender = bool.Parse(ds.Tables[0].Rows[0][3].ToString()),
                    Mobile = ds.Tables[0].Rows[0][4].ToString(),
                    ClassName = ds.Tables[0].Rows[0][5].ToString()
                };

                this.textBoxName.Text = vm.Name;
                this.textBoxClassName.Text = vm.ClassName;
                if (vm.Gender)
                    this.radioButtonMale.Checked = true;
                else
                    this.radioButtonFemale.Checked = true;

                /// 实现用户相片的读取
                GetStuPic(vm.UserID.ToString());

            }
            else
            {
                MessageBox.Show("没有符合此学号的用户信息！请查证后，重新输入！", "系统提示");
                this.textBoxStuNo.Clear();//清空学号输入文本框
                this.textBoxStuNo.Focus();//光标定位至学号输入文本框
            }

        }

        private void buttonBorrowBook_Click(object sender, EventArgs e)
        {
            Guid stuid = vm.UserID;//获取用户ID值
            string bookid = this.textBoxBookID.Text.Trim();//获取用户输入的索书号
            if (stuid == Guid.Empty)
            {
                MessageBox.Show("请输入学生数据后，再进行借书操作！", "系统提示");
                this.textBoxStuNo.Clear();
                this.textBoxStuNo.Focus();
            }
            else
            {
                bool result = bll.GetUserBorrowInfo(stuid, bookid);
                if (result)
                {
                    MessageBox.Show("已成功借阅书籍！", "系统提示");
                    this.textBoxBookID.Clear();
                    this.textBoxBookID.Focus();
                    // 刷新借阅列表
                }
                else
                    MessageBox.Show("借阅书籍失败！请选择其他书籍完成借阅操作", "系统提示");
            }
        }
    }
}
